package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.FmedItem;
import his.persistence.FmedItemMapper;
import his.service.FmedItemService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TimeZone;

@Service
public class FmedItemServiceImpl implements FmedItemService {

    @Autowired
    FmedItemMapper fmedItemMapper;

    @Override
    public CommonResponse<List<FmedItem>> delete_fmeditems(String item_code) {

        QueryWrapper<FmedItem> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ItemCode",item_code);
        List<FmedItem> fmedItemList=fmedItemMapper.selectList(queryWrapper1);
        if(fmedItemList.size()>0){
            QueryWrapper<FmedItem> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ItemCode",item_code);
            int result=fmedItemMapper.delete(queryWrapper2);
            return CommonResponse.createForSuccess(fmedItemList);
        }else{
            return CommonResponse.createForError("删除失败");
        }

    }

    public static Date parseDateTimeString(String dateTimeString, String pattern) {
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat(pattern);
        try {
            // 将字符串解析为日期
            Date parsedDateTime = dateTimeFormat.parse(dateTimeString);
            return parsedDateTime;
        } catch (ParseException e) {
            // 解析失败，处理异常情况
            e.printStackTrace();
            return null;
        }
    }

    public static String convertUTCtoLocal(String utcTimeString, String utcPattern, String localPattern) {
        SimpleDateFormat utcFormat = new SimpleDateFormat(utcPattern);
        utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

        SimpleDateFormat localFormat = new SimpleDateFormat(localPattern);
        try {
            // 将 UTC 时间解析为 Date 对象
            Date utcDate = utcFormat.parse(utcTimeString);

            // 设置本地时区
            localFormat.setTimeZone(TimeZone.getDefault());

            // 将 Date 对象格式化为本地时间字符串
            String localTimeString = localFormat.format(utcDate);
            return localTimeString;
        } catch (ParseException e) {
            // 解析失败，处理异常情况
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public CommonResponse<List<FmedItem>> getall_fmeditems_bycodename(String keywords) {

        QueryWrapper<FmedItem> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("MnemonicCode",keywords);
        List<FmedItem> fmedItemList1=fmedItemMapper.selectList(queryWrapper1);
        if(fmedItemList1.size()>0){
            return CommonResponse.createForSuccess(fmedItemList1);
        }else{
            QueryWrapper<FmedItem> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ItemName",keywords);
            List<FmedItem> fmedItemList2=fmedItemMapper.selectList(queryWrapper2);
            if(fmedItemList2.size()>0){
                return CommonResponse.createForSuccess(fmedItemList2);
            }else{
                return CommonResponse.createForError("搜索失败");
            }
        }

    }

    @Override
    public CommonResponse<List<FmedItem>> update_fmeditems( String item_code, String itemName, String format, BigDecimal price, int expClassID, int deptID, String mnemonicCode, int recordType) {

        QueryWrapper<FmedItem> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ItemCode",item_code);
        List<FmedItem> fmedItemList1=fmedItemMapper.selectList(queryWrapper1);
        if(fmedItemList1.size()>0){
            QueryWrapper<FmedItem> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ItemCode",item_code);
            FmedItem fmedItem=new FmedItem();

            fmedItem.setItemName(itemName);
            fmedItem.setFormat(format);
            fmedItem.setPrice(price);
            fmedItem.setExpClassID(expClassID);
            fmedItem.setDeptID(deptID);
            fmedItem.setMnemonicCode(mnemonicCode);

            Date lastUpdateDate1 = new Date();

            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String temp=dateFormat.format(lastUpdateDate1);


            fmedItem.setLastUpdateDate(new Date());
            fmedItem.setRecordType(recordType);
            int result=fmedItemMapper.update(fmedItem,queryWrapper2);
            QueryWrapper<FmedItem> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("ID",fmedItemList1.get(0).getID());
            List<FmedItem> fmedItemList3=fmedItemMapper.selectList(queryWrapper3);
            return CommonResponse.createForSuccess(fmedItemList3);
        }else{
            return CommonResponse.createForError("修改失败");
        }

    }

    @Override
    public CommonResponse<List<FmedItem>> add_fmeditems(String itemCode, String itemName, String format, BigDecimal price, int expClassID, int deptID, String mnemonicCode, int recordType) {

        QueryWrapper<FmedItem> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ItemCode",itemCode);
        List<FmedItem> fmedItemList1=fmedItemMapper.selectList(queryWrapper1);
        if(fmedItemList1.size()>0){
            return CommonResponse.createForError("新增失败，编号已存在");
        }else{
            QueryWrapper<FmedItem> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ItemName",itemName);
            List<FmedItem> fmedItemList2=fmedItemMapper.selectList(queryWrapper2);
            if(fmedItemList2.size()>0){
                return CommonResponse.createForError("新增失败，名称已存在");
            }else{
                FmedItem fmedItem=new FmedItem();
                fmedItem.setItemCode(itemCode);
                fmedItem.setItemName(itemName);
                fmedItem.setFormat(format);
                fmedItem.setPrice(price);
                fmedItem.setExpClassID(expClassID);
                fmedItem.setDeptID(deptID);
                fmedItem.setMnemonicCode(mnemonicCode);




                fmedItem.setCreationDate(new Date());
//                fmedItem.setLastUpdateDate(lastUpdateDate);
                fmedItem.setRecordType(recordType);
                fmedItem.setDelMark(1);
                fmedItemMapper.insert(fmedItem);

                QueryWrapper<FmedItem> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("ItemCode",itemCode);
                List<FmedItem> fmedItemList3=fmedItemMapper.selectList(queryWrapper3);
                return CommonResponse.createForSuccess(fmedItemList3);
            }
        }

    }

    @Override
    public CommonResponse<List<FmedItem>> getall_fmeditems() {

        QueryWrapper<FmedItem> queryWrapper=new QueryWrapper<>();
        List<FmedItem> fmedItemList=fmedItemMapper.selectList(queryWrapper);
        if(fmedItemList.size()>0){

            return CommonResponse.createForSuccess(fmedItemList);
        }else{
            return CommonResponse.createForError("查询失败");
        }

    }


}
